package cn.line5.seam.cms.column;

import java.util.List;

import cn.line5.module.ModuleVo;
import cn.line5.organization.OrganizationConstants;
import cn.line5.organization.OrganizationVo;
import cn.line5.seam.cms.column.ColumnVo;
import cn.line5.seam.annotations.In;
import cn.line5.seam.annotations.Name;
import cn.line5.seam.annotations.Transaction;
import cn.line5.seam.db.IDGenerator;
import cn.line5.seam.db.PageResult;

@Name("columnService")
/**
 * <h1>LSEAM框架示例业务层实现类</h1>
 * 
 * @author Liu,yandong
 */
public class ColumnServiceImpl implements ColumnService{
	@In("columnDao")
	private ColumnDao columnDao;
	
	public List<ColumnVo> getList(){
		return this.columnDao.getList();
	}
	
	public PageResult<ColumnVo> getPageList(int currentPage){
		return this.columnDao.getPageList(currentPage);
	}
	
	public ColumnVo getColumn(String id){
		return this.columnDao.getColumn(id);
	}
	
	public ColumnVo getColumnVo(ColumnVo columnVo){
		return this.columnDao.getColumnVo(columnVo);
	}
	
	public boolean saveColumn(ColumnVo vo){
		if(vo.getColumnId() != null){
			return this.columnDao.modifyColumn(vo);
		}else{
			vo.setColumnId(IDGenerator.UUID());
			return this.columnDao.insertColumn(vo);
		}
	}

	@Transaction
	public boolean deleteColumn(String[] ids){
		boolean t = true;
		for(String id : ids){
			t &= this.columnDao.deleteColumn(id);
		}
		return t;
	}
	/**
	 * 机构向上排序
	 */
	@Transaction
	public Boolean upColumn(String parentColumnId,String columnId) {
		List<ColumnVo> list = columnDao.getColumnList(parentColumnId);
		
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				ColumnVo vo1 = list.get(i);
				if (vo1.getColumnId().equals(columnId)) {
					if (i > 0) {
						ColumnVo vo2 = list.get(i - 1);
						Integer columnSort = vo1.getColumnSort();
						vo1.setColumnSort(vo2.getColumnSort());
						vo2.setColumnSort(columnSort);
						return columnDao.saveColumnSort(vo1)
								&& columnDao.saveColumnSort(vo2);
					}
					break;
				}
			}
		}
		return false;
	}

	/**
	 * 机构向下排序
	 */
	@Transaction
	public Boolean downColumn(String parentColumnId,String columnId) {
		List<ColumnVo> list = columnDao.getColumnList(parentColumnId);
		
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				ColumnVo vo1 = list.get(i);
				if (vo1.getColumnId().equals(columnId)) {
					if (i < list.size() - 1) {
						ColumnVo vo2 = list.get(i + 1);
						Integer columnSort = vo1.getColumnSort();
						vo1.setColumnSort(vo2.getColumnSort());
						vo2.setColumnSort(columnSort);
						return columnDao.saveColumnSort(vo1)
								&& columnDao.saveColumnSort(vo2);
					}
					break;
				}
			}
		}
		return false;
	}
	
	/**
	 * 获取树的JS代码
	 */
	public String getColumnTreeJs() {
		ColumnVo vo = getRootColumn();
		return new StringBuffer().append(
				"var tree = new MzTreeView(\"tree\");\n").append(
				"tree.setIconPath(\"/admin/include/images/MzTree/\");\n").append(
				"tree.nodes[\"-1_").append(vo.getColumnId()).append(
				"\"] = \"text:").append(vo.getColumnName()).append("\";\n")
				.append(getColumnTreeJs(vo.getColumnId())).append(
						"tree.setURL(\"moduleMain.seam\");\n").append(
						"tree.setTarget(\"moduleMain\");\n").append(
						"document.write(tree.toString());\n").toString();
	}

	/**
	 * 获取根机构信息
	 */
	private ColumnVo getRootColumn() {
		ColumnVo vo = new ColumnVo();
		vo.setColumnId("0");
		vo.setColumnName("栏目节点");
		vo.setColumnSort(0);
		return vo;
	}	

	/**
	 * 获取机构树JS代码-递归方法
	 */
	private String getColumnTreeJs(String parentId) {
		StringBuffer sb = new StringBuffer();
		List<ColumnVo> list = columnDao.getColumnList(!"0".equals(parentId) ? parentId : "0");
		if (list != null && list.size() > 0) {
			for (ColumnVo vo : list) {
				sb.append("tree.nodes[\"").append(parentId).append("_").append(
						vo.getColumnId()).append("\"] = \"text:").append(
						vo.getColumnName()).append(
						"; data:columnId=")
						.append(vo.getColumnId()).append("\";\n").append(
								getColumnTreeJs(vo.getColumnId()));
			}
		}
		return sb.toString();
	}	
	
}
